#include "qlogdatabase.h"

#include "qsqlitecontrol.h"

#include <QApplication>
#include <QStringList>

QLogDatabase::QLogDatabase():
    m_sqlite_control(new QSqliteControl)
{
}

QLogDatabase::~QLogDatabase()
{
    delete m_sqlite_control;
}

void QLogDatabase::open(const QString &path)
{
    m_sqlite_control->open(path+"/log_data.db");
    QStringList list=m_sqlite_control->get_tables();
    if(!list.contains("property"))
    {
        m_sqlite_control->create_table("property",QStringList()<<"name varchar(32) primary key"<<"value varchar(48)");
    }
}

void QLogDatabase::set_property(const QString &key, const QString &value)
{
    m_sqlite_control->replace("property",QStringList()<<key<<value);
}

void QLogDatabase::close()
{
    m_sqlite_control->close();
}

void QLogDatabase::add_log(const QStringList &log)
{
    if(!m_sqlite_control->get_tables().contains("Log"))
    {
        m_sqlite_control->create_table("Log",QStringList()<<"time varchar(32) primary key"<<"user varchar(32)"
                                       <<"tag varchar(32)"<<"message varchar(256)");
    }
    m_sqlite_control->insert("Log",log);
}
